#include "ThreadPool.h"
#include <iostream>
using namespace std;

const int MAX_TASKS = 2;

void hello(void* arg)
{
	char* x = (char*) arg;
	cout << "Hello " << *x << endl;
}

int main(int argc, char* argv[])
{
	CMyThreadPool tp;
	int ret = tp.Initialize();
	if (ret == -1) 
	{
		cerr << "Failed to initialize thread pool!" << endl;
		return 0;
	}

	int n = 20000;
	while (n--)
	{
		printf("----------------------------\n");
		for (int i = 0; i < MAX_TASKS; i++)
		{
			//int* x = new int();
			//*x = i + 1;
			char* x = new char[1024];
			strcpy(x,"aaaaa");
			CTask* t = new CTask(&hello, (void*)x);

			bool loop = false;
			do
			{
				if (!tp.AddTask(t))
				{
					SLEEP(10);
					loop = true;
				}

			} while (loop);

			cout << "Add to pool, task " << i + 1 << endl;
		}
	}
	tp.WaitAll();
	cout << "Exiting Application..." << endl;

	return 0;
}
